﻿namespace ComTools.File
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;

    /// <summary>
    /// XML帮助类
    /// </summary>
    public class XmlHelper
    {
        /// <summary>
        /// 创建一个xml文档
        /// </summary>
        /// <returns></returns>
        public static XmlDocument Create()
        {
            XmlDocument result = new XmlDocument();
            result.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><DOC></DOC>");
            return result;
        }

        /// <summary>
        /// 初始为一个空的xml
        /// </summary>
        /// <param name="xDoc"></param>
        public static void Init(XmlDocument xDoc)
        {
            xDoc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><DOC></DOC>");
            return;
        }

        /// <summary>
        /// 调用xml的LoadXml方法
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static bool LoadFromXml(XmlDocument xDoc, string xml)
        {
            bool result = false;
            try
            {
                xDoc.LoadXml(xml);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 调用xml的LoadXml方法,创建一个XMLDocument
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static XmlDocument LoadFromXml(string xml)
        {
            XmlDocument result = XmlHelper.Create();
            try
            {
                if (!XmlHelper.LoadFromXml(result, xml))
                {
                    result = null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 从调用xml的Load方法
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool LoadFromFile(XmlDocument xDoc, string filename)
        {
            bool result = false;
            try
            {
                if (File.Exists(filename))
                {
                    xDoc.Load(filename);
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 从调用xml的Load方法,创建一个XMLDocument
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static XmlDocument LoadFromFile(string filename)
        {
            XmlDocument result = XmlHelper.Create();
            try
            {
                if (!XmlHelper.LoadFromFile(result, filename))
                {
                    result = null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        ///  根据结点名称,查找子结点列表
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <param name="first"></param>
        /// <returns></returns>
        public static List<XmlNode> GetChild(XmlNode parent, string name, bool first)
        {
            List<XmlNode> result = new List<XmlNode>();

            if (parent != null && parent.NodeType == XmlNodeType.Element)
            {
                for (XmlNode node = parent.FirstChild; node != null; node = node.NextSibling)
                {
                    if (node.NodeType == XmlNodeType.Element
                        && string.Compare(node.Name, name, true) == 0)
                    {
                        result.Add(node);
                        if (first)
                        {
                            break;
                        }
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 根据结点名称,查找子结点列表
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static List<XmlNode> GetChildren(XmlNode parent, string name)
        {
            return XmlHelper.GetChild(parent, name, false);
        }

        /// <summary>
        /// 根据结点名称,查找第一个子结点
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static XmlNode GetChild(XmlNode parent, string name)
        {
            XmlNode result = null;
            if (parent != null && parent.NodeType == XmlNodeType.Element)
            {
                List<XmlNode> nodes = XmlHelper.GetChild(parent, name, true);
                if (nodes.Count > 0)
                {
                    result = nodes[0];
                }
            }
            return result;
        }

        /// <summary>
        /// 添加一个结点
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <param name="unique"></param>
        /// <returns></returns>
        public static XmlNode AddChild(XmlNode parent, string name, bool unique)
        {
            XmlNode result = null;
            if (parent != null && parent.NodeType == XmlNodeType.Element)
            {
                if (unique)
                {// 唯一个名字称的子结点
                    result = XmlHelper.GetChild(parent, name);
                }
                if (result == null)
                {
                    result = parent.OwnerDocument.CreateElement(name);
                    parent.AppendChild(result);
                }
            }
            return result;
        }

        /// <summary>
        /// 添加一个结点,名称唯一
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static XmlNode AddChild(XmlNode parent, string name)
        {
            return XmlHelper.AddChild(parent, name, true);
        }

        /// <summary>
        /// 根据路径返回结点列表
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="fullpath"></param>
        /// <returns></returns>
        public static XmlNode GetChildByFullpath(XmlNode parent, string fullpath)
        {
            XmlNode result = null;
            List<XmlNode> rList = XmlHelper.GetChildrenByFullpath(parent, fullpath);
            if (rList.Count > 0)
            {
                result = rList[0];
            }
            return result;
        }

        /// <summary>
        /// 根据路径返回结点列表
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="fullpath"></param>
        /// <returns></returns>
        public static List<XmlNode> GetChildrenByFullpath(XmlNode parent, string fullpath)
        {
            List<XmlNode> result = new List<XmlNode>();

            string[] arrPaths = fullpath.Split('\\');
            int icount = arrPaths.Length;

            if (icount > 0)
            {
                result.Add(parent);
                bool bRan = false;
                for (int i = 0; i < icount; i++)
                {
                    string sPath = arrPaths[i];
                    if (sPath == null || sPath.Trim().Length == 0)
                    {
                        continue;
                    }
                    else if (!bRan)
                    {
                        bRan = true;
                    }
                    sPath = sPath.Trim();

                    List<XmlNode> r1 = new List<XmlNode>();
                    foreach (XmlNode node in result)
                    {
                        r1.AddRange(XmlHelper.GetChild(node, sPath, false));
                    }
                    result.Clear();
                    result.AddRange(r1);
                }
                if (!bRan)
                {
                    result.Clear();
                }
            }
            return result;
        }

        /// <summary>
        ///根路径添加一个结点,该结点遵循名称唯一性
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="fullpath"></param>
        /// <returns></returns>
        public static XmlNode AddChildByFullpath(XmlNode parent, string fullpath)
        {
            XmlNode result = null;

            string[] arrPaths = fullpath.Split('\\');
            int icount = arrPaths.Length;

            if (icount > 0)
            {
                result = null;
                for (int i = 0; i < icount; i++)
                {
                    string sPath = arrPaths[i];
                    if (sPath == null || sPath.Trim().Length == 0)
                    {
                        continue;
                    }
                    else
                    {
                        sPath = sPath.Trim();
                        result = XmlHelper.AddChild(parent, sPath);
                        parent = result;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 移出某个结点中指定名称的所有子结点
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        public static void RemoveChildren(XmlNode parent, string name)
        {
            List<XmlNode> list = XmlHelper.GetChild(parent, name, false);
            foreach (XmlNode node in list)
            {
                parent.RemoveChild(node);
            }
            return;
        }

        //-------------------------------------------//
        //                   Text                    //
        //-------------------------------------------//
        /// <summary>
        /// 在子结点中,添加一个文本结点,如果已存在,则替换内容
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static XmlNode AddText(XmlNode parent, string text)
        {
            XmlNode result = null;
            if (parent != null && parent.NodeType == XmlNodeType.Element)
            {
                for (XmlNode node = parent.FirstChild; node != null; node = node.NextSibling)
                {
                    if (node.NodeType == XmlNodeType.Text)
                    {
                        result = node;
                        break;
                    }
                }
                if (result == null)
                {
                    result = parent.OwnerDocument.CreateTextNode(text);
                    parent.AppendChild(result);
                }
                else
                {
                    result.Value = text;
                }
            }
            return result;
        }

        public static XmlNode AddText(XmlNode parent, Int32 value)
        {
            return XmlHelper.AddText(parent, Convert.ToString(value));
        }

        public static XmlNode AddText(XmlNode parent, Int64 value)
        {
            return XmlHelper.AddText(parent, Convert.ToString(value));
        }

        public static XmlNode AddText(XmlNode parent, float value)
        {
            return XmlHelper.AddText(parent, Convert.ToString(value));
        }

        public static XmlNode AddText(XmlNode parent, Double value)
        {
            return XmlHelper.AddText(parent, Convert.ToString(value));
        }

        public static XmlNode AddText(XmlNode parent, Decimal value)
        {
            return XmlHelper.AddText(parent, Convert.ToString(value));
        }

        public static XmlNode AddText(XmlNode parent, Boolean value)
        {
            return XmlHelper.AddText(parent, value ? "true" : "false");
        }



        /// <summary>
        /// 添加一个子结点,并且,同时添加一个text
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <param name="text"></param>
        /// <param name="unique"></param>
        /// <returns></returns>
        public static XmlNode AddText(XmlNode parent, string name, string text, bool unique)
        {
            XmlNode result = null;
            if (parent != null)
            {
                result = XmlHelper.AddText(XmlHelper.AddChild(parent, name, unique), text);
            }
            return result;
        }

        public static XmlNode AddText(XmlNode parent, string name, Int32 value, bool unique)
        {
            return XmlHelper.AddText(parent, name, Convert.ToString(value), unique);
        }

        public static XmlNode addText(XmlNode parent, string name, Int64 value, bool unique)
        {
            return XmlHelper.AddText(parent, name, Convert.ToString(value), unique);
        }

        public static XmlNode AddText(XmlNode parent, string name, float value, bool unique)
        {
            return XmlHelper.AddText(parent, name, Convert.ToString(value), unique);
        }

        public static XmlNode AddText(XmlNode parent, string name, Double value, bool unique)
        {
            return XmlHelper.AddText(parent, name, Convert.ToString(value), unique);
        }

        public static XmlNode AddText(XmlNode parent, string name, Decimal value, bool unique)
        {
            return XmlHelper.AddText(parent, name, Convert.ToString(value), unique);
        }

        public static XmlNode AddText(XmlNode parent, string name, Boolean value, bool unique)
        {
            return XmlHelper.AddText(parent, name, value ? "true" : "false", unique);
        }



        /// <summary>
        /// 添加一个唯一名称子结点,并且,同时添加一个text
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static XmlNode AddText(XmlNode parent, string name, string text)
        {
            return XmlHelper.AddText(parent, name, text, true);
        }

        public static XmlNode AddText(XmlNode parent, string name, Int32 value)
        {
            return XmlHelper.AddText(parent, name, System.Convert.ToString(value));
        }

        public static XmlNode AddText(XmlNode parent, string name, Int64 value)
        {
            return XmlHelper.AddText(parent, name, System.Convert.ToString(value));
        }

        public static XmlNode AddText(XmlNode parent, string name, float value)
        {
            return XmlHelper.AddText(parent, name, System.Convert.ToString(value));
        }

        public static XmlNode AddText(XmlNode parent, string name, Double value)
        {
            return XmlHelper.AddText(parent, name, System.Convert.ToString(value));
        }

        public static XmlNode AddText(XmlNode parent, string name, Decimal value)
        {
            return XmlHelper.AddText(parent, name, System.Convert.ToString(value));
        }

        public static XmlNode AddText(XmlNode parent, string name, Boolean value)
        {
            return XmlHelper.AddText(parent, name, value ? "true" : "false");
        }


        /// <summary>
        ///  查询一个结点的第一文本结点内容
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static string GetText(XmlNode node)
        {
            string result = "";
            if (node != null && node.NodeType == XmlNodeType.Element)
            {
                for (XmlNode xn = node.FirstChild; xn != null; xn = xn.NextSibling)
                {
                    if (xn.NodeType == XmlNodeType.Text)
                    {
                        result = xn.Value;
                    }
                }
            }
            return result;
        }

        public static Int32 GetTextAsInt(XmlNode node)
        {
            return Convert.ToInt32(XmlHelper.GetText(node));
        }

        public static Int32 GetTextAsInt32(XmlNode node)
        {
            return Convert.ToInt32(XmlHelper.GetText(node));
        }

        public static Int64 GetTextAsInt64(XmlNode node)
        {
            return Convert.ToInt64(XmlHelper.GetText(node));
        }

        public static Double GetTextAsDouble(XmlNode node)
        {
            return Convert.ToDouble(XmlHelper.GetText(node));
        }

        public static Decimal GetTextAsDecimal(XmlNode node)
        {
            return Convert.ToDecimal(XmlHelper.GetText(node));
        }

        public static Boolean GetTextAsBool(XmlNode node)
        {
            return string.Compare(XmlHelper.GetText(node), "true", true) == 0;
        }

        public static DateTime GetTextAsDateTime(XmlNode node)
        {
            return Convert.ToDateTime(XmlHelper.GetText(node));
        }

        /// <summary>
        /// 查询一个指定子结点名称的第一个文本结点内容
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetText(XmlNode node, string name)
        {
            string result = "";
            if (node != null)
            {
                result = XmlHelper.GetText(XmlHelper.GetChild(node, name));
            }
            if (result != string.Empty)
            {
                result = result.Replace('<', '〈');
                result = result.Replace('>', '〉');
            }
            return result;
        }


        public static Int32 GetTextAsInt(XmlNode node, string name)
        {
            return Convert.ToInt32(XmlHelper.GetText(node, name));
        }

        public static Int32 GetTextAsInt32(XmlNode node, string name)
        {
            return Convert.ToInt32(XmlHelper.GetText(node, name));
        }

        public static Int64 GetTextAsInt64(XmlNode node, string name)
        {
            return Convert.ToInt64(XmlHelper.GetText(node, name));
        }

        public static Double GetTextAsDouble(XmlNode node, string name)
        {
            return Convert.ToDouble(XmlHelper.GetText(node, name));
        }

        public static Decimal GetTextAsDecimal(XmlNode node, string name)
        {
            return Convert.ToDecimal(XmlHelper.GetText(node, name));
        }

        public static Boolean GetTextAsBool(XmlNode node, string name)
        {
            return string.Compare(XmlHelper.GetText(node, name), "true", true) == 0;
        }

        public static DateTime GetTextAsDateTime(XmlNode node, string name)
        {
            return Convert.ToDateTime(XmlHelper.GetText(node, name));
        }

        //-------------------------------------------//
        //                   CData                   //
        //-------------------------------------------//
        /// <summary>
        /// 添加一个唯一名称子结点,并且,同时添加一个CData
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static XmlNode AddCData(XmlNode parent, string data)
        {
            XmlNode result = null;
            if (string.IsNullOrEmpty(data))
            {
                return result;
            }

            if (parent != null && parent.NodeType == XmlNodeType.Element)
            {
                for (XmlNode node = parent.FirstChild; node != null; node = node.NextSibling)
                {
                    if (node.NodeType == XmlNodeType.CDATA)
                    {
                        result = node;
                        break;
                    }
                }
                if (result == null)
                {
                    result = parent.OwnerDocument.CreateCDataSection(data);
                    parent.AppendChild(result);
                }
                else
                {
                    result.Value = data;
                }
            }
            return result;
        }

        /// <summary>
        /// 添加一个唯一名称子结点,并且,同时添加一个CData
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static XmlNode AddCData(XmlNode parent, byte[] data)
        {
            XmlNode result = null;
            if (data != null && data.Length > 0 && parent != null && parent.NodeType == XmlNodeType.Element)
            {
                for (XmlNode node = parent.FirstChild; node != null; node = node.NextSibling)
                {
                    if (node.NodeType == XmlNodeType.CDATA)
                    {
                        result = node;
                        break;
                    }
                }
                if (result == null)
                {
                    result = parent.OwnerDocument.CreateCDataSection(System.Convert.ToBase64String(data, Base64FormattingOptions.None));
                    parent.AppendChild(result);
                }
                else
                {
                    result.Value = Convert.ToBase64String(data);
                }
            }
            return result;
        }

        /// <summary>
        /// 添加一个子结点,并且,同时添加一个cdata
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <param name="data"></param>
        /// <param name="unique"></param>
        /// <returns></returns>
        public static XmlNode AddCData(XmlNode parent, string name, string data, bool unique)
        {
            XmlNode result = null;
            if (parent != null)
            {
                result = XmlHelper.AddCData(XmlHelper.AddChild(parent, name, unique), data);
            }
            return result;
        }

        public static XmlNode AddCData(XmlNode parent, string name, byte[] data, bool unique)
        {
            XmlNode result = null;
            if (parent != null)
            {
                result = XmlHelper.AddCData(XmlHelper.AddChild(parent, name, unique), data);
            }
            return result;
        }

        /// <summary>
        /// 添加一个唯一名称子结点,并且,同时添加一个CData
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static XmlNode AddCData(XmlNode parent, string name, string data)
        {
            return XmlHelper.AddCData(parent, name, data, true);
        }

        public static XmlNode AddCData(XmlNode parent, string name, byte[] data)
        {
            return XmlHelper.AddCData(parent, name, data, true);
        }

        /// <summary>
        /// 查询一个结点的第一文本结点内容
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static string GetCData(XmlNode node)
        {
            string result = "";
            if (node != null && node.NodeType == XmlNodeType.Element)
            {
                for (XmlNode xn = node.FirstChild; xn != null; xn = xn.NextSibling)
                {
                    if (xn.NodeType == XmlNodeType.CDATA)
                    {
                        result = xn.Value;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 查询一个结点的第一文本结点内容
        /// </summary>
        /// <param name="node"></param>
        /// <param name="value"></param>
        public static void GetCData(XmlNode node, ref byte[] value)
        {
            if (node != null && node.NodeType == XmlNodeType.Element)
            {
                for (XmlNode xn = node.FirstChild; xn != null; xn = xn.NextSibling)
                {
                    if (xn.NodeType == XmlNodeType.CDATA)
                    {
                        value = Convert.FromBase64String(xn.Value);
                    }
                }
            }
            return;
        }

        /// <summary>
        /// 查询一个指定子结点名称的第一个CData结点内容
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetCData(XmlNode node, string name)
        {
            string result = "";
            if (node != null)
            {
                result = XmlHelper.GetCData(XmlHelper.GetChild(node, name));
            }
            return result;
        }

        public static void GetCData(XmlNode node, string name, ref byte[] value)
        {
            if (node != null)
            {
                XmlHelper.GetCData(XmlHelper.GetChild(node, name), ref value);
            }
            return;
        }

        /// <summary>
        /// 复制子结点
        /// </summary>
        /// <param name="dest"></param>
        /// <param name="source"></param>
        public static void Copy(XmlNode dest, XmlNode source)
        {
            if (source == null || source.NodeType != XmlNodeType.Element
                || dest == null || dest.NodeType != XmlNodeType.Element)
            {
                return;
            }

            // 复制属性
            foreach (XmlAttribute attr in source.Attributes)
            {
                XmlHelper.SetAttribute(dest, attr.Name, attr.Value);
            }

            // 复制子结点
            for (XmlNode child = source.FirstChild; child != null; child = child.NextSibling)
            {
                XmlNode nChild = dest.OwnerDocument.CreateNode(child.NodeType, child.Name, child.NamespaceURI);
                dest.AppendChild(nChild);
                nChild.InnerXml = child.InnerXml;
            }
            return;
        }

        //-------------------------------------------//
        //                Attribute                  //
        //-------------------------------------------//

        /// <summary>
        /// 查询一个Attribute结点
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static XmlAttribute GetAttribute(XmlNode node, string name)
        {
            XmlAttribute result = null;
            if (node != null && node.NodeType == XmlNodeType.Element)
            {
                foreach (XmlAttribute attr in node.Attributes)
                {
                    if (string.Compare(attr.Name, name, true) == 0)
                    {
                        result = attr;
                        break;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 设置Attribute结点值,如果结点不存在,则添加一个
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static XmlAttribute SetAttribute(XmlNode node, string name, string value)
        {
            XmlAttribute result = null;
            if (node != null && node.NodeType == XmlNodeType.Element)
            {
                result = XmlHelper.GetAttribute(node, name);
                if (result == null)
                {
                    result = node.OwnerDocument.CreateAttribute(name);
                    result.Value = value;
                    node.Attributes.Append(result);
                }
            }
            return result;
        }

        /// <summary>
        /// 设置Attribute结点值,如果结点不存在,则添加一个
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static XmlAttribute SetAttribute(XmlNode node, string name, Int32 value)
        {
            return XmlHelper.SetAttribute(node, name, System.Convert.ToString(value));
        }

        /// <summary>
        /// 设置Attribute结点值,如果结点不存在,则添加一个
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static XmlAttribute SetAttribute(XmlNode node, string name, Boolean value)
        {
            string sValue = "";
            if (value)
            {
                sValue = "true";
            }
            else
            {
                sValue = "false";
            }
            return XmlHelper.SetAttribute(node, name, sValue);
        }

        /// <summary>
        /// 设置Attribute结点值,如果结点不存在,则添加一个
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static XmlAttribute SetAttribute(XmlNode node, string name, Int64 value)
        {
            return XmlHelper.SetAttribute(node, name, System.Convert.ToString(value));
        }

        /// <summary>
        /// 设置Attribute结点值,如果结点不存在,则添加一个
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static XmlAttribute SetAttribute(XmlNode node, string name, Decimal value)
        {
            return XmlHelper.SetAttribute(node, name, System.Convert.ToString(value));
        }

        /// <summary>
        ///  查询一个Attribute结点,并以字符串返回值
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetAttrAsString(XmlNode node, string name)
        {
            string result = "";
            XmlAttribute attr = XmlHelper.GetAttribute(node, name);
            if (attr != null)
            {
                result = attr.Value;
            }
            return result;
        }

        /// <summary>
        /// 查询一个Attribute结点,并以32位整型返回值
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Int32 GetAttrAsInt32(XmlNode node, string name)
        {
            Int32 result = 0;
            XmlAttribute attr = XmlHelper.GetAttribute(node, name);
            if (attr != null)
            {
                result = Convert.ToInt32(attr.Value);
            }
            return result;
        }

        /// <summary>
        /// 查询一个Attribute结点,并以64位整型返回值
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Int64 GetAttrAsInt64(XmlNode node, string name)
        {
            Int64 result = 0;
            XmlAttribute attr = XmlHelper.GetAttribute(node, name);
            if (attr != null)
            {
                result = Convert.ToInt64(attr.Value);
            }
            return result;
        }

        /// <summary>
        /// 查询一个Attribute结点,并以逻辑值返回
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool GetAttrAsBool(XmlNode node, string name)
        {
            bool result = false;
            XmlAttribute attr = XmlHelper.GetAttribute(node, name);
            if (attr != null)
            {
                result = string.Compare(attr.Value, "true", true) == 0;
            }
            return result;
        }

        /// <summary>
        /// 查询一个Attribute结点,并以十进制数返回
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Decimal GetAttrAsDecimal(XmlNode node, string name)
        {
            Decimal result = 0.0M;
            XmlAttribute attr = XmlHelper.GetAttribute(node, name);
            if (attr != null)
            {
                result = Convert.ToDecimal(attr.Value);
            }
            return result;
        }

        /// <summary>
        /// 移出一个Attribute
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        public static void RemoveAttribute(XmlNode node, string name)
        {
            XmlAttribute attr = XmlHelper.GetAttribute(node, name);
            if (node != null)
            {
                if (attr != null)
                {
                    node.Attributes.Remove(attr);
                }
            }
            return;
        }
    }
}
